package slxy.jiangtao.dts.service.sys;

import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import slxy.jiangtao.dts.core.common.hibernate.qbc.CriteriaQuery;
import slxy.jiangtao.dts.core.common.service.CommonService;
import slxy.jiangtao.dts.entity.sys.Menu;
import slxy.jiangtao.dts.entity.sys.ZtreeMenu;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * Created by JiangTao on 2017/1/20.
 */
@Service("menuSevice")
public class MenuSevice {

    private static final Logger log = LoggerFactory.getLogger(UserService.class);
    @Autowired
    private CommonService commonService;

    /**
     * 根据主键获取一个菜单
     */
    public Menu getById(Serializable id) {
        return commonService.getEntity(Menu.class, id);
    }

    /**
     * 根据菜单级别获取菜单集
     */
    public List<Menu> getMenuByLevel(Integer level) {
        CriteriaQuery cq = new CriteriaQuery(Menu.class);
        cq.add(Restrictions.eq("level", level));
        return commonService.getListByCriteriaQuery(cq);
    }

    /**
     * 添加菜单,并自动排序
     */
    public int add(Menu menu) {
        Integer parentid = menu.getParentId();
        if (parentid != null) {
            CriteriaQuery cq = new CriteriaQuery(Menu.class);
            cq.add(Restrictions.eq("id", parentid));
            menu.setSequence(commonService.getListByCriteriaQuery(cq).size() + 1); // 同级菜单总数加1
        } else { // 如果没有父级菜单说明为一级菜单
            menu.setSequence(getMenuByLevel(1).size() + 1);
        }
        return (int) commonService.save(menu);
    }

    /**
     * 菜单二叉树，用于权限菜单修改
     */
    public List<ZtreeMenu> getZtreeMenu(Integer roleInd) {
        List<Menu> rootMenu = commonService.getList(Menu.class);
        rootMenu.sort(Comparator.comparing(Menu::getSequence));
        //获得已有权限的菜单id集合
        List<Integer> menuIds = commonService.findListbySql("select menu_id from role_menu where role_id = " + roleInd);
        List<ZtreeMenu> ztreeMenuList = new ArrayList<>();
        Optional.ofNullable(rootMenu).ifPresent(
                menus ->
                        menus.forEach(
                                menu -> {
                                    ZtreeMenu ztreeMenu = new ZtreeMenu();
                                    ztreeMenu.setId(menu.getId());
                                    ztreeMenu.setName(menu.getName());
                                    ztreeMenu.setPid(menu.getParentId());
                                    if (menuIds.contains(menu.getId())) {
                                        ztreeMenu.setChecked(true);
                                    } else {
                                        ztreeMenu.setChecked(false);
                                    }
                                    ztreeMenuList.add(ztreeMenu);
                                }
                        )
        );
        return ztreeMenuList;
    }

    /**
     * 根据角色获取菜单集合，用于页面权限控制
     */
    public List<Menu> getRoleMenus(Integer roleInd) {
        List<Menu> rootMenu = commonService.getList(Menu.class);
        rootMenu.sort(Comparator.comparing(Menu::getSequence));
        //获得已有权限的菜单id集合
        List<Integer> menuIds = commonService.findListbySql("select menu_id from role_menu where role_id = " + roleInd);
        List<Menu> menuList = new ArrayList<>();
        Optional.ofNullable(rootMenu).ifPresent(
                menus ->
                        menus.forEach(
                                menu -> {
                                    if (menuIds.contains(menu.getId())) {
                                        menuList.add(menu);
                                    }
                                }
                        )
        );
        return menuList;
    }

    public boolean deleteRoleMenu(Integer roleId) {
        String sql = "DELETE FROM role_menu WHERE role_id = ?";
        Integer re = commonService.executeSql(sql, roleId);
        return re < 0 ? false : true;
    }
}
